package approve.adapter;

import java.util.ArrayList;
import java.util.List;

import model.pojo.Employee;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import approve.action.App_ViewForm12;
import approve.action.App_ViewForm13;
import approve.action.App_ViewForm14;
import approve.action.App_ViewForm15;
import approve.action.App_ViewForm16;
import approve.action.App_ViewForm17;
import approve.action.App_ViewForm_LessThan10;
import approve.action.R;
import approve.dao.NotApproveDAO;
import approve.model.DepartmentLang;
import approve.model.ListEmployee;

public class App_Pop_AdapterApproverAll extends BaseAdapter{
	Context context;
	List<ListEmployee> listchoosetapprover= new ArrayList<ListEmployee>();
	List<Employee> listCheckRefer = new ArrayList<Employee>();
	String stridrefer;
	String strid;
	String lang="EN";
	String db;
	Bundle language;
	ListEmployeeReferAdapter lstemadapter = new ListEmployeeReferAdapter();
	ListEmployeeAdapter emadapter_chooserefer1 = new ListEmployeeAdapter();
	int screenheight;
	int flagcontext;
	
	public App_Pop_AdapterApproverAll(App_ViewForm17 context,List<ListEmployee> listchoosetapprover,String lang, String db,Bundle language,int screenheight,int flag){
		this.context=context;
		this.listchoosetapprover=listchoosetapprover;
		this.lang=lang;
		this.db=db;
		this.language=language;
		this.screenheight=screenheight;
		this.flagcontext=flag;
	}
	
	public App_Pop_AdapterApproverAll(App_ViewForm16 context,List<ListEmployee> listchoosetapprover,String lang, String db,Bundle language,int screenheight,int flag){
		this.context=context;
		this.listchoosetapprover=listchoosetapprover;
		this.lang=lang;
		this.db=db;
		this.language=language;
		this.screenheight=screenheight;
		this.flagcontext=flag;
	}
	
	public App_Pop_AdapterApproverAll(App_ViewForm15 context,List<ListEmployee> listchoosetapprover,String lang, String db,Bundle language,int screenheight, int flag){
		this.context=context;
		this.listchoosetapprover=listchoosetapprover;
		this.lang=lang;
		this.db=db;
		this.language=language;
		this.screenheight=screenheight;
		this.flagcontext=flag;
	}
	
	public App_Pop_AdapterApproverAll(App_ViewForm14 context,List<ListEmployee> listchoosetapprover,String lang, String db,Bundle language,int screenheight,int flag){
		this.context=context;
		this.listchoosetapprover=listchoosetapprover;
		this.lang=lang;
		this.db=db;
		this.language=language;
		this.screenheight=screenheight;
		this.flagcontext=flag;
	}
	
	public App_Pop_AdapterApproverAll(App_ViewForm13 context,List<ListEmployee> listchoosetapprover,String lang, String db,Bundle language,int screenheight,int flag){
		this.context=context;
		this.listchoosetapprover=listchoosetapprover;
		this.lang=lang;
		this.db=db;
		this.language=language;
		this.screenheight=screenheight;
		this.flagcontext=flag;
	}
	
	public App_Pop_AdapterApproverAll(App_ViewForm12 context,List<ListEmployee> listchoosetapprover,String lang, String db,Bundle language,int screenheight,int flag){
		this.context=context;
		this.listchoosetapprover=listchoosetapprover;
		this.lang=lang;
		this.db=db;
		this.language=language;
		this.screenheight=screenheight;
		this.flagcontext=flag;
	}
	
	public App_Pop_AdapterApproverAll(App_ViewForm_LessThan10 context,List<ListEmployee> listchoosetapprover,String lang, String db,Bundle language,int screenheight,int flag){
		this.context=context;
		this.listchoosetapprover=listchoosetapprover;
		this.lang=lang;
		this.db=db;
		this.language=language;
		this.screenheight=screenheight;
		this.flagcontext=flag;
	}
	
	public List<Employee> getListCheckRefer() {
		return listCheckRefer;
	}



	public void setListCheckRefer(List<Employee> listCheckRefer) {
		this.listCheckRefer = listCheckRefer;
	}



	public String getStridrefer() {
		stridrefer="";
		if(listCheckRefer!=null && listCheckRefer.size()>0){
			for(int i=0;i<listCheckRefer.size();i++){
				if(stridrefer.equals("")){
					stridrefer="'"+listCheckRefer.get(i).getIdemployee()+"'";
				}else{
					stridrefer+=",'"+listCheckRefer.get(i).getIdemployee()+"'";
				}
			}
		}
		return stridrefer;
	}

	public void setStridrefer(String stridrefer) {
		this.stridrefer = stridrefer;
	}

	public List<ListEmployee> getListchoosetapprover() {
		return listchoosetapprover;
	}

	public void setListchoosetapprover(List<ListEmployee> listchoosetapprover) {
		this.listchoosetapprover = listchoosetapprover;
	}


	public String getStrid() {
		strid="";
		if(listchoosetapprover!=null && listchoosetapprover.size()>0){
			for(int i=0;i<listchoosetapprover.size();i++){
				if(strid.equals("")){
					strid="'"+listchoosetapprover.get(i).getEmpapprover().getIdemployee()+"'";
				}else{
					strid+=",'"+listchoosetapprover.get(i).getEmpapprover().getIdemployee()+"'";
				}
			}
		}
		return strid;
	}



	public void setStrid(String strid) {
		this.strid = strid;
	}



	@Override
	public int getCount() {
		return listchoosetapprover.size();
	}
	@Override
	public Object getItem(int arg0) {
		return listchoosetapprover.get(arg0);
	}
	@Override
	public long getItemId(int arg0) {
		return arg0;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(convertView==null){
			LayoutInflater a=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = a.inflate(R.layout.app_pop_adapterapproverall, parent,false);
		}
		final ListEmployee oneemp= (ListEmployee) listchoosetapprover.get(position);
		final ImageView	imi_choosedelete =(ImageView)convertView.findViewById(R.id.imi_choosedelete);	
		final TextView chb=(TextView)convertView.findViewById(R.id.chb);
		final ImageView imi=(ImageView)convertView.findViewById(R.id.imi);
		final ListView lstchooserefer=(ListView)convertView.findViewById(R.id.listchooserefer);
		
		chb.setText(oneemp.getEmpapprover().getFullName());

		if(flagcontext==15){
			lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm15) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
		}else if(flagcontext==14){
			lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm14) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
		}else if(flagcontext==13){
			lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm13) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
		}else if(flagcontext==12){
			lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm12) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
		}else if(flagcontext==16){
			lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm16) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
		}else if(flagcontext==17){
			lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm17) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
		}else if(flagcontext==10){
			lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm_LessThan10) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
		}
		
		
		lstchooserefer.setAdapter(lstemadapter);
		listCheckRefer.addAll(oneemp.getListrefer());
		
		if(oneemp.getListrefer()!=null && oneemp.getListrefer().size()>0){
			lstchooserefer.setVisibility(View.VISIBLE);
			int totalHeight1 = 0;
	        for (int i = 0; i < lstemadapter.getCount(); i++) {
	            View listItem = lstemadapter.getView(i, null, lstchooserefer);
	            listItem.measure(0, 0);
	            totalHeight1 += listItem.getMeasuredHeight();
	        }

	        ViewGroup.LayoutParams params1= lstchooserefer.getLayoutParams();
	        params1.height = totalHeight1 + (lstchooserefer.getDividerHeight() * (lstchooserefer.getCount() - 1));
	        lstchooserefer.setLayoutParams(params1);
		}else{
			lstchooserefer.setVisibility(View.GONE);
		}
		
		imi_choosedelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				oneemp.getEmpapprover().setChecked(false);
				if(oneemp.getListrefer()!=null && oneemp.getListrefer().size()>0){
					listCheckRefer.removeAll(oneemp.getListrefer());
				}
				listchoosetapprover.remove(oneemp);
				
				if(flagcontext==15){
					((App_ViewForm15) context).getApproverlist();
					((App_ViewForm15) context).getChooseapproveradapter().notifyDataSetChanged();
				}else if(flagcontext==14){
					((App_ViewForm14) context).getApproverlist();
					((App_ViewForm14) context).getChooseapproveradapter().notifyDataSetChanged();
				}else if(flagcontext==13){
					((App_ViewForm13) context).getApproverlist();
					((App_ViewForm13) context).getChooseapproveradapter().notifyDataSetChanged();
				}else if(flagcontext==12){
					((App_ViewForm12) context).getApproverlist();
					((App_ViewForm12) context).getChooseapproveradapter().notifyDataSetChanged();
				}else if(flagcontext==16){
					((App_ViewForm16) context).getApproverlist();
					((App_ViewForm16) context).getChooseapproveradapter().notifyDataSetChanged();
				}else if(flagcontext==17){
					((App_ViewForm17) context).getApproverlist();
					((App_ViewForm17) context).getChooseapproveradapter().notifyDataSetChanged();
				}else if(flagcontext==10){
					((App_ViewForm_LessThan10) context).getApproverlist();
					((App_ViewForm_LessThan10) context).getChooseapproveradapter().notifyDataSetChanged();
				}
			}
		});
		
		imi.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);          
				View view = inflater.inflate(R.layout.app_pop_addapprover, null);
				final Spinner spindepartment1=(Spinner)view.findViewById(R.id.spin_department);
				final ListView lvrefer=(ListView)view.findViewById(R.id.lv_approver);
				
				LayoutParams laypara1 = (LayoutParams) lvrefer.getLayoutParams();
				laypara1.height=screenheight - 100;
				lvrefer.setLayoutParams(laypara1);
				
				ArrayList<DepartmentLang> lstpopdepart1=NotApproveDAO.getListDepartment(lang, db);
				ArrayAdapter<DepartmentLang> departadapter1=new ArrayAdapter<DepartmentLang>(context,android.R.layout.simple_spinner_dropdown_item,lstpopdepart1);
				departadapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				spindepartment1.setAdapter(departadapter1);
				
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setTitle(language.getString("selecttherefer"));
				builder.setCancelable(false);
				builder.setView(view);
				
				spindepartment1.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						DepartmentLang departmentchoose1 = (DepartmentLang)arg0.getItemAtPosition(arg2);
						ArrayList<Employee> emlist_chooserefer1= NotApproveDAO.getListEmployeeSubRefer(departmentchoose1.getId(),db,getStridrefer());
						emadapter_chooserefer1= new ListEmployeeAdapter(context,emlist_chooserefer1);
						lvrefer.setAdapter(emadapter_chooserefer1);
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						
					}
				});
				
				builder.setNegativeButton(language.getString("cancel"),
						new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog,
							int which) {

					}
				});
				
				builder.setPositiveButton(language.getString("ok"), new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						List<Employee> temrefer= emadapter_chooserefer1.getListCheck();
						if(temrefer!=null && temrefer.size()>0){
							lstchooserefer.setVisibility(View.VISIBLE);
							oneemp.getListrefer().addAll(temrefer);
							
							if(flagcontext==15){
								lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm15) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
							}else if(flagcontext==14){
								lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm14) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
							}else if(flagcontext==13){
								lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm13) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
							}else if(flagcontext==12){
								lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm12) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
							}else if(flagcontext==16){
								lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm16) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
							}else if(flagcontext==17){
								lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm17) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
							}else if(flagcontext==10){
								lstemadapter =  new ListEmployeeReferAdapter((App_ViewForm_LessThan10) context, (ArrayList<Employee>) oneemp.getListrefer(),flagcontext);
							}
							
							listCheckRefer.addAll(oneemp.getListrefer());
							lstchooserefer.setAdapter(lstemadapter);
							
							int totalHeight1 = 0;
					        for (int i = 0; i < lstemadapter.getCount(); i++) {
					            View listItem = lstemadapter.getView(i, null, lstchooserefer);
					            listItem.measure(0, 0);
					            totalHeight1 += listItem.getMeasuredHeight();
					        }

					        ViewGroup.LayoutParams params1= lstchooserefer.getLayoutParams();
					        params1.height = totalHeight1 + (lstchooserefer.getDividerHeight() * (lstchooserefer.getCount() - 1));
					        lstchooserefer.setLayoutParams(params1);
						}
					}
				});
				
				

				final AlertDialog alert = builder.create();
				alert.show();
				
			}
		});
		
		
		return convertView;
	}

}
